package com.server.http.handler.impl;

import com.server.conditionenum.HttpReturnStatus;
import com.server.db.DataBaseManager;
import com.server.http.handler.HttpHandler;
import com.server.http.request.HttpRequestMessage;
import com.server.http.response.HttpResponseMessage;
import com.server.mine.net.HttpProtocol;
import com.server.tools.AppData;
import com.server.tools.Tools;

/**
* @author	李小布
* @email	397554852@qq.com
* @version	创建时间：2018年9月27日 下午4:51:03
*/
public class SaveData extends HttpHandler{
	// -1上传参数错误;-2APP不存在;-3数据库异常;-4未知错误;-5解密失败
	@Override
	public void handle(HttpRequestMessage request, HttpResponseMessage response) {
		int failMsg = 0;
		long uid = 0;
		long appid = 0;
		String key = request.getParameter("key");
		String data = request.getParameter("data");
		if(key == null || "".equals(key) || data == null || "".equals(data)) {
			failMsg = -1;
		}else {
			try {
				uid =Long.parseLong(request.getParameter("uid"));
				appid = Long.parseLong(request.getParameter("appid"));
				if(!AppData.appMap.containsKey(appid)) failMsg = -2;
			} catch (Exception e) {
				failMsg = -5;
			}
		}
		
		if(failMsg == 0) {
			String value = Tools.compress(data, AppData.appMap.get(appid).appkey);
			failMsg = DataBaseManager.GetInstance().SaveData(uid, appid, key, value);
			System.out.println(failMsg);
		}
		
		if(failMsg == 0) {
			SetReturnData(response,1,HttpProtocol.SAVEDATA,HttpReturnStatus.Successed);
		}else {
			SetReturnData(response,failMsg,HttpProtocol.SAVEDATA,HttpReturnStatus.Failed);
		}
		
	}

}
